
define([
  './custom/widgets/js/yufpOrgTree.js',
  './custom/plugins/YufpMgrSelector.js',
  './custom/widgets/js/yufpExtTree.js'

], function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    // 注册该功能要用到的数据字典
    yufp.lookup.reg('DATA_STS,SYS_TYPE,YES_NO,SEX_TYPE,SCENE_TYPE,CARD_TYPE,CUST_LEVEL,BLG_IDY,OCP_CD');
    // 创建virtual model
    yufp.custom.vue({
      el: '#gridUserQuery',
      // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
      data: function () {
        return {
          judgeUser:true,
          options1:[],
          options2:[],
          custInfo:{
            isDocument:'',
            sceneType:''
          },
          custData:{condition: JSON.stringify({
              userId: yufp.session.userId,
              orgId: yufp.session.user.org.code,
              roleId:yufp.session.user.roles[0].code,
            })},
          funcDataUrl:backend.msmFrontWebService + '/api/CustCenter/getAllCustomerInformation',
          uploadData:{condition: JSON.stringify({
              userId: yufp.session.userId,
              orgId: yufp.session.orgId,
            })},
          headers: {
            'Authorization': 'Bearer' + yufp.service.getToken()
          },
          dialogFormVisible: false,
          dialogStatus: '',
          saveBtnShow:'',
          formDisabled: false,
          loading: false,
        };
      },
      created:function (){
        this.judgeUserFn()
      },
      // 钩子
      mounted: function () {
        this.getGridName()
      },
      methods: {
        judgeUserFn:function (){
          var me =this;
          const roleId = yufp.session.user.roles[0].code;
          const orgId = yufp.session.user.org.code;
          if (roleId ==="R0008" || roleId === "R0007" || roleId === "R001"|| orgId=== "070667XN0" || orgId==="070667XN1" ||orgId==="070667860" ||orgId==="070667XN3"){
            me.judgeUser= true;
          }
          else {
            me.judgeUser= false;
            me.getSecondGridName();
          }
        },
        changeCustOrgId:function (){
          this.$refs.customerInfoSearch.formdata.custGridId = '';
          this.getSecondGridName()
        },
        getGridName:function (){
          var me =this;
          yufp.service.request({
            url: backend.msmFrontWebService + '/api/CustCenter/getGridName',
            method: 'get',
            data: {
              condition: JSON.stringify({
                userId: yufp.session.userId,
                orgId: yufp.session.user.org.code,
                roleId:yufp.session.user.roles[0].code,
              })
            },
            callback: function (code, message, response) {
              me.options1 = response.data
            }
          });
        },
        getSecondGridName:function (){
          var me =this;
          yufp.service.request({
            url: backend.msmFrontWebService + '/api/CustCenter/getSecondGridName',
            method: 'get',
            data: {
              condition: JSON.stringify({
                userId: yufp.session.userId,
                orgId: yufp.session.user.org.code,
                roleId:yufp.session.user.roles[0].code,
                preGrid: me.judgeUser=== true ? me.$refs.customerInfoSearch.formdata.custOrgId : ''
              })
            },
            callback: function (code, message, response) {
              me.options2 = response.data
            }
          });
        },
        searchFn: function () {
          var me = this;
          var param = {
            condition: JSON.stringify({
              custName: me.$refs.customerInfoSearch.formdata.custName,
              custLevel: me.$refs.customerInfoSearch.formdata.custLevel,
              custGridId: me.$refs.customerInfoSearch.formdata.custGridId,
              gridId: me.$refs.customerInfoSearch.formdata.custOrgId,
              ctfNo: me.$refs.customerInfoSearch.formdata.ctfNo,
              custManager: me.$refs.customerInfoSearch.formdata.custManager
              // userName: me.$refs.customerInfoSearch.formdata.userName,
            })
          };
          me.$refs.khtable.remoteData(param);
        },
        updateImport:function (isImport,custNo){
          var me =this;
          yufp.service.request({
            url: backend.msmFrontWebService + '/api/CustCenter/updateCustomerImport',
            method: 'get',
            data: {
              condition: JSON.stringify({
                isImport:isImport,
                crtOprtrNo:yufp.session.userId,
                custNo:custNo
              })
            },
            callback: function (code, message, response) {
              me.restInputValue();
              me.$refs.khtable.remoteData();
              if (code === 0) {
                if (isImport === "0"){
                  me.$message({message: '添加重点客户成功', type: 'success'});
                }
                if(isImport === "1"){
                  me.$message({message: '取消重点客户成功', type: 'success'});
                }
                //获取上级网格名称和ID
              } else {
                me.$message({message: '重点客户变更失败', type: 'warning'});
              }
            }
          });},
        //每次执行显示操作，新增操作执行清空，修改执行查询，
        restInputValue:function (){
          var _this = this;
          _this.dialogFormVisible = false;
          _this.$nextTick(function () {
            if (_this.$refs.custForm){
              _this.$refs.custForm.resetFields();
            }
          });
        },
        lookupInformation:function (){
          var _this =this;
          if (this.$refs.khtable.selections.length !== 1) {
            this.$message({message: '请选择一个选项', type: 'warning'});
            return;
          }
          _this.restInputValue();
          _this.switchStatus('detail', false);
          yufp.service.request({
            url: backend.msmFrontWebService + '/api/CustCenter/getCustomerInformation',
            method: 'get',
            data: {
              condition: JSON.stringify({
                crtOprtrNo:yufp.session.userId,
                custNo:this.$refs.khtable.selections[0].custNo
              })
            },
            callback: function (code, message, response) {
              if (code === 0) {
                _this.custInfo.custName = response.data[0].custName;
                _this.custInfo.isDocument = response.data[0].isDocument;
                _this.custInfo.ctfTp = response.data[0].ctfTp;
                _this.custInfo.ctfNo = response.data[0].ctfNo;
                _this.custInfo.gndrCd = response.data[0].gndrCd;
                _this.custInfo.address = response.data[0].address;
                _this.custInfo.mobNo = response.data[0].mobNo;
                _this.custInfo.entpIdyCgy = response.data[0].entpIdyCgy;
                _this.custInfo.ocpCd = response.data[0].ocpCd;
                _this.custInfo.sceneType = response.data[0].sceneType;
                _this.custInfo.witchTypeValue = response.data[0].witchUnit.substr(0, response.data[0].witchUnit.length - 1);
              } else {
              }
            }
          });
        },
        switchStatus: function (viewType, editable) {
          var _this = this;
          _this.dialogStatus = viewType;
          _this.saveBtnShow = editable;
          _this.dialogFormVisible = true;
          _this.formDisabled = !editable;
        },
        //客户视图
        toCustView: function (scope) {
          var _this = this;
          var selectedCustInfo = null;
          if (scope == undefined && scope == null) {
            _this.$message({ message: '客户信息错误!', type: 'warning' });
            return;
          }else{
            selectedCustInfo = scope;
          }
          var custInfo = {'custNo': selectedCustInfo.custNo,
            'custName': selectedCustInfo.custName
          };
          // 生成tab页签及路由ID
          var customKey = 'custom_view' + selectedCustInfo.custNo; // 请以custom_view前缀开头，并且全局唯一
          var routeId = 'custView' + selectedCustInfo.custNo; // 路由ID，设置唯一，以便消息传递，并且不允许修改，客户视图内的页面间通信会用到
          // 传递的业务数据
          var popageData = {
            id: '123', // 视图树节点id
            cust: custInfo
          };
          var toPageurl = 'pages/example/custView/custView';
          var r = yufp.router.getRoute(routeId);
          var t = new Date().getTime();
          if (!r || !r.html) {
            yufp.router.addRoute(routeId, {
              html: toPageurl+'.html',
              js: toPageurl+'.js'
            });
          }
          yufp.frame.addTab({
            id: routeId, // 菜单功能ID（路由ID）
            key: customKey, // 自定义唯一页签key,请统一使用custom_前缀开头
            title: '客户:' + selectedCustInfo.custName, // 页签名称
            data: popageData // 传递的业务数据，可选配置
          });
        }
      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) { };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {};
});
